package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 套餐
 *
 * @Author Yang
 * @Date 2023/3/5 16:23
 */
@RestController
@Api(tags = "套餐")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private DishService dishService;

    //分页查询
    @GetMapping("/setmeal/page")
    public ResultInfo findPage(@RequestParam(value = "page") Integer pageNum,
                               @RequestParam(value = "pageSize") Integer pageSize,
                               String name){
        Page<Setmeal> page = setmealService.findPage(pageNum,pageSize,name);
        return ResultInfo.success(page);
    }

    //新增套餐
    @CacheEvict(value = "setmeal",key ="#setmeal.categoryId")
    @PostMapping("/setmeal")
    public ResultInfo save(@RequestBody Setmeal setmeal){
        setmealService.save(setmeal);
        return ResultInfo.success();
    }

    //Id回显
    @GetMapping("/setmeal/{id}")
    public ResultInfo findById(@PathVariable("id") long id){
        Setmeal setmeal = setmealService.findById(id);
        return ResultInfo.success(setmeal);
    }

    //修改
    @PutMapping("/setmeal")
    @CacheEvict(value = "setmeal",allEntries = true)
    public ResultInfo updateSetmeal(@RequestBody Setmeal setmeal){
        setmealService.updateSetmeal(setmeal);
        return ResultInfo.success();
    }

    //更改起售-停售

    @PostMapping("/setmeal/status/{status}")
    @CacheEvict(value = "setmeal",allEntries = true)
    public ResultInfo updateStatus(@PathVariable(value = "status") Integer status,
                                   @RequestParam List<Long> ids){
        setmealService.updateStatus(status,ids);
        return ResultInfo.success();
    }

    // 删除套餐
    @DeleteMapping("/setmeal")
    @CacheEvict(value = "setmeal",allEntries = true)
    public ResultInfo deleteBatch(@RequestParam List<Long> ids) { // 1.接收参数
        // 2.调用service删除
        setmealService.deleteBatch(ids);
        // 3.返回结果
        return ResultInfo.success();
    }




























}
